package com.lou.springboot.controller.admin;

import com.lou.springboot.common.ResultVO;
import com.lou.springboot.common.ResultVOUtil;
import com.lou.springboot.entity.BlogLink;
import com.lou.springboot.enums.ResultEnum;
import com.lou.springboot.service.BlogLinkService;
import com.lou.springboot.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class LinkController {

    @Autowired
    private BlogLinkService linkService;

    @GetMapping("/links")
    public String linkPage(HttpServletRequest request) {
        request.setAttribute("path", "links");
        return "admin/link";
    }

    @GetMapping("/links/list")
    @ResponseBody
    public ResultVO list(@RequestParam Map<String, Object> map) {
        if (StringUtils.isEmpty(map.get("page")) || StringUtils.isEmpty(map.get("limit"))) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        PageUtil pageUtil = new PageUtil(map);
        return ResultVOUtil.success(linkService.getBlogLinkPage(pageUtil));
    }

    @PostMapping("/links/save")
    @ResponseBody
    public ResultVO save(@RequestParam Integer linkType,
                         @RequestParam String linkName,
                         @RequestParam String linkUrl,
                         @RequestParam String linkDescription,
                         @RequestParam Integer linkRank) {
        if(StringUtils.isEmpty(linkType) || linkType < 0) {
            return ResultVOUtil.error("友链类型错误");
        }
        if(StringUtils.isEmpty(linkName)) {
            return ResultVOUtil.error("网站名称错误");
        }
        if(StringUtils.isEmpty(linkUrl)) {
            return ResultVOUtil.error("友链链接错误");
        }
        if(StringUtils.isEmpty(linkDescription)) {
            return ResultVOUtil.error("友链类型错误");
        }
        if(StringUtils.isEmpty(linkRank) || linkRank < 0) {
            return ResultVOUtil.error("排序值错误");
        }
        BlogLink link = new BlogLink();
        link.setLinkType(linkType);
        link.setLinkName(linkName);
        link.setLinkUrl(linkUrl);
        link.setLinkDescription(linkDescription);
        link.setLinkRank(linkRank);
        if (linkService.saveBlogLink(link)){
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error("创建失败");
        }
    }

    @PostMapping("/links/update")
    @ResponseBody
    public ResultVO update(@RequestParam Integer linkId,
                           @RequestParam Integer linkType,
                           @RequestParam String linkName,
                           @RequestParam String linkUrl,
                           @RequestParam String linkDescription,
                           @RequestParam Integer linkRank) {
        if(StringUtils.isEmpty(linkId) || linkId < 1) {
            return ResultVOUtil.error("友链id错误");
        }
        if(StringUtils.isEmpty(linkType) || linkType < 0) {
            return ResultVOUtil.error("友链类型错误");
        }
        if(StringUtils.isEmpty(linkName)) {
            return ResultVOUtil.error("网站名称错误");
        }
        if(StringUtils.isEmpty(linkUrl)) {
            return ResultVOUtil.error("友链链接错误");
        }
        if(StringUtils.isEmpty(linkDescription)) {
            return ResultVOUtil.error("友链类型错误");
        }
        if(StringUtils.isEmpty(linkRank) || linkRank < 0) {
            return ResultVOUtil.error("排序值错误");
        }
        BlogLink link = new BlogLink();
        link.setLinkId(linkId);
        link.setLinkName(linkName);
        link.setLinkType(linkType);
        link.setLinkUrl(linkUrl);
        link.setLinkDescription(linkDescription);
        link.setLinkRank(linkRank);
        if (linkService.updateBlogLink(link)){
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error("更新失败");
        }
    }

    @GetMapping("/links/info/{linkId}")
    @ResponseBody
    public ResultVO getLinkById(@PathVariable Integer linkId){
        if (linkId == null || linkId < 1) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        BlogLink link = linkService.getBlogLinkById(linkId);
        if (link == null) {
            return ResultVOUtil.error("友链不存在");
        } else {
            return ResultVOUtil.success(link);
        }
    }

    @PutMapping("/links/delete")
    @ResponseBody
    public ResultVO deleteBatch(@RequestBody Integer[] linkIds){
        if (linkIds.length < 1) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        if (linkService.deleteBatch(linkIds)) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error("删除失败");
        }
    }
}
